//
//  HCHomeMiddleView.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/30.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HCHomeMiddleView.h"

#import "HCHomeMiddleCollectionViewCell.h"

@interface HCHomeMiddleView () <UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource> {
    NSArray *arr_images;
    NSArray *arr_texts;
}

@end

@implementation HCHomeMiddleView

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        [self initSubViewsFrame:frame];
    }
    return self;
}

-(void)initSubViewsFrame:(CGRect)frame {
    
    arr_texts = @[@"成长相册",@"在线预约",@"育儿宝箱",@"消息中心"];
    arr_images = @[[UIImage imageNamed:@"happy_home_photos"],[UIImage imageNamed:@"happy_home_appointment"],[UIImage imageNamed:@"home_treasureChest"],[UIImage imageNamed:@"happy_home_messages"]];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    self.homeMidCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:flow];
    self.homeMidCollectionView.backgroundColor = [UIColor whiteColor];
    flow.minimumInteritemSpacing = (frame.size.width - YBKAdaptiveW(60)*4)/8.0;
    flow.minimumLineSpacing = 0;
    self.homeMidCollectionView.delegate = self;
    self.homeMidCollectionView.dataSource = self;
    self.homeMidCollectionView.scrollEnabled = NO;
    [self addSubview:self.homeMidCollectionView];
    
    [self.homeMidCollectionView registerNib:[UINib nibWithNibName:@"HCHomeMiddleCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cellhomecollection"];
}

#pragma mark UICollectionDelgate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(YBKAdaptiveW(60), YBKAdaptiveH(65));
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HCHomeMiddleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellhomecollection" forIndexPath:indexPath];
    [cell cellModelImages:arr_images texts:arr_texts indexPath:indexPath];
    
    NSLog(@"宽度%f",[UIScreen mainScreen].bounds.size.width);
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(YBKAdaptiveH(8), (Screen_width-YBKAdaptiveH(60)*4.0)/8.0, 0, (Screen_width-YBKAdaptiveH(60)*4.0)/8.0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if ([self.delegate respondsToSelector:@selector(middleViewButtonsClicked:)]&&self.delegate) {
        switch (indexPath.row) {
            case 0:
                [self.delegate middleViewButtonsClicked:HCHomeMiddleViewImages];
                break;
            case 1:
                [self.delegate middleViewButtonsClicked:HCHomeMiddleViewOnline];
                break;
            case 2:
                [self.delegate middleViewButtonsClicked:HCHomeMiddleViewKnowledge];
                break;
            case 3:
                [self.delegate middleViewButtonsClicked:HCHomeMiddleViewMessages];
                break;
            default:
                break;
        }
    }
    
}

@end
